Page({
  data: {
    fileId: ''
  },
  // 上传文件
  chooseFile() {
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success: res => {
        console.log(res)
        let temFile = res.tempFiles[0]
        this.uploadFile(temFile.name, temFile.path)
      }
    })    
  },

  // 上传选中文件
  uploadFile(name, tempUrl) {
    wx.cloud.uploadFile({
      cloudPath: name,
      filePath: tempUrl,
      success: res => {
        console.log("上传成功", res)
      }
    })
  },

  // 获取用户输入的下载链接
  getContent(e){
    this.setData({
      fileId: e.detail.value
    })
  },

  // 点击下载
  downloadFile(){
    let fileId = this.data.fileId
    if(fileId != null && fileId.length >0){
      wx.cloud.downloadFile({
        fileID: fileId
      })
      .then(res => {
        console.log("下载成功", res)
        wx.openDocument({
          filePath: res.tempFilePath,
          success: res => {
            console.log("成功打开文件",res)
          }
        })
      })
      .catch(err => {
        console.error("下载失败", err)
      })
    }else{
      wx.showToast({
        title: '下载链接不能为空',
        icon: 'none'
      })
    }
  }

})